import { messages, dealCache } from "share/common";
/**
 * Created By zaranengap on 2017/09/25
 */
import React from 'react'
import { connect } from 'react-redux'
import { setBudgetBalance } from "actions/cache"
import configureStore from "stores"

import { Button, Form, Row, Col, Input, Select, Spin, Affix, Table, Popconfirm, Modal, message, Checkbox } from 'antd'
const FormItem = Form.Item;
const Option = Select.Option;
import debounce from 'lodash.debounce';
import Chooser from 'components/chooser'
import SlideFrame from 'components/slide-frame'
import BudgetBalanceCondition from 'containers/budget/budget-balance/budget-balance-condition'
import menuRoute from 'routes/menuRoute'
import chooserData from 'share/chooserData'
import Condition from 'components/condition'
const { If } = Condition;

import 'styles/budget/budget-balance/budget-balance.scss'
import httpFetch from 'share/httpFetch'
import config from 'config'
import budgetBalanceService from 'containers/budget/budget-balance/badget-balance.service'

class BudgetBalance extends React.Component {
  constructor(props) {
    super(props);
    this.loadNum = 0;
    this.state = {
      structureId: null,
      tableLoading: false,
      showSlideFrame: false,
      params: [],
      queryLineListTypeOptions: [],
      queryLineListParamOptions: {},
      columns: [
        {title: messages('budget.key58'/*参数类型*/), dataIndex: 'type', width: '20%', render: (text, record, index) => this.renderColumns(index, 'type')},
        {title: messages('budget.key59'/*参数*/), dataIndex: 'params', width: '35%', render: (text, record, index) => this.renderColumns(index, 'params')},
        {title: messages('budget.key60'/*参数值*/), dataIndex: 'value', width: '35%', render: (text, record, index) => this.renderColumns(index, 'value')},
        {title: messages('budget.key37'/*操作*/), dataIndex: 'operation', width: '10%', render: (text, record, index) => (
          <span>
            <Popconfirm onConfirm={(e) => this.deleteItem(e, index)} title={messages('budget.key39'/*你确定要删除这条数据吗？*/)}>
              <a onClick={(e) => {e.preventDefault();e.stopPropagation();}}>{messages('common.delete')/* 删除 */}</a>
            </Popconfirm>
          </span>)}
      ],
      searchForm: [],
      organizationId: props.organization.id,
      paramsKey: 0,
      paramTypeMap: {
        'BGT_RULE_PARAMETER_BUDGET': 2015,
        'BGT_RULE_PARAMETER_ORG': 2016
      },
      paramValueMap: {},
      showSaveModal: false,
      conditionCode: '',
      conditionName: '',
      condition: null,
      saving: false,
      searching: false,
      saveNewCondition: true,
      costCenterSelectorItem: {  //成本中心所需要的selectorItem，url需要在params的onChange里手动添加
        listType: 'support_failure_cost_center',
        labelKey: 'name',
        valueKey: 'id',
        codeKey: 'code',
        listExtraParams: undefined,
        selectorItem: undefined
      },
      periodYear: '',
    };
    this.setOptionsToFormItem = debounce(this.setOptionsToFormItem, 250);
  }

  componentWillMount(){
    let { queryLineListTypeOptions, organizationId ,queryHalfYearTypeOptions} = this.state;
    queryHalfYearTypeOptions = [];
    this.getSystemValueList(2012).then(res => {
      queryLineListTypeOptions = [];
      res.data.values && res.data.values.map(data => {
        queryLineListTypeOptions.push({ label: data.messageKey, value: data.code })
      });
      this.setState({ queryLineListTypeOptions })
    });
    this.getSystemValueList(2031)
      .then(res => {
        res.data.values && res.data.values.map(data => {
          queryHalfYearTypeOptions.push({
            label: data.messageKey,
            value: data.value,
            key: data.value
          });
        });
      });
    let nowYear = new Date().getFullYear();
    let yearOptions = [];
    for(let i = nowYear - 4; i <= nowYear + 20; i++)
      yearOptions.push({label: i, key: i});
    let organizationIdParams = { organizationId };
    let searchForm = [
      {type: 'select', id:'versionId', label: messages('budget.key53'/*预算版本*/), isRequired: true, options: [], method: 'get',
        getUrl: `${config.budgetUrl}/api/budget/versions/queryAll`, getParams: organizationIdParams,
        labelKey: 'versionName', valueKey: 'id'},
      {type: 'select', id:'structureId', label: messages('budget.key55'/*预算表*/), isRequired: true, options: [], method: 'get',
        getUrl: `${config.budgetUrl}/api/budget/structures/queryAll`, getParams: organizationIdParams, event: 'STRUCTURE_CHANGE',
        labelKey: 'structureName', valueKey: 'id'},
      {type: 'select', id:'scenarioId', label: messages('budget.key56'/*预算场景*/), isRequired: true, options: [], method: 'get',
        getUrl: `${config.budgetUrl}/api/budget/scenarios/queryAll`, getParams: organizationIdParams,
        labelKey: 'scenarioName', valueKey: 'id'},
      {type: 'select', id:'yearLimit', activeValue: nowYear + '',label: messages('budget.key7'/*年度*/), options: yearOptions,
        event: 'YEAR_CHANGE', isRequired: true},
      {type: 'select', id:'semiannualLimit',label: messages('budget.key165')/*半年度*/, options: queryHalfYearTypeOptions, isRequired: true},
      {type: 'items', id: 'dateRange', items: [
        {type: 'select', id: 'periodLowerLimit', label: messages('budget.key61'/*期间从*/), options: [], method: 'get', disabled: true,
        getUrl: `${config.baseUrl}/api/company/group/assign/query/budget/periods`, getParams: {setOfBooksId: this.props.company.setOfBooksId},
          labelKey: 'periodName', valueKey: 'periodName'},
        {type: 'select', id: 'periodUpperLimit', label: messages('budget.key62'/*期间到*/), options: [], method: 'get', disabled: true,
          getUrl: `${config.baseUrl}/api/company/group/assign/query/budget/periods`, getParams: {setOfBooksId: this.props.company.setOfBooksId},
          labelKey: 'periodName', valueKey: 'periodName'}
      ]},
      {type: 'items', id: 'seasonRange', items: [
        {type: 'value_list', id: 'quarterLowerLimit', label: messages('budget.key63'/*季度从*/), options: [], valueListCode: 2021, disabled: true},
        {type: 'value_list', id: 'quarterUpperLimit', label: messages('budget.key64'/*季度到*/), options: [], valueListCode: 2021, disabled: true}
      ]},
      {type: 'value_list', id:'periodSummaryFlag', label: messages('budget.key65'/*期段汇总*/), options: [], valueListCode: 2020, disabled: true},
      {type: 'value_list', id:'amountQuarterFlag', label: messages('budget.key54'/*金额 / 数量*/), isRequired: true, options: [], valueListCode: 2019}
    ];

    let itemSelectorItem = chooserData['budget_item'];
    itemSelectorItem.listExtraParams = organizationIdParams;
    itemSelectorItem.searchForm[1].getParams = itemSelectorItem.searchForm[2].getParams = organizationIdParams;

    let userSelectorItem = chooserData['support_failure_user'];
    userSelectorItem.key = 'id';

    let paramValueMap = {
      'BUDGET_ITEM_TYPE': {
        listType: 'budget_item_type',
        labelKey: 'itemTypeName',
        valueKey: 'id',
        codeKey: 'itemTypeCode',
        listExtraParams: organizationIdParams,
        selectorItem: undefined
      },
      'BUDGET_ITEM_GROUP': {
        listType: 'budget_item_group',
        labelKey: 'itemGroupName',
        valueKey: 'id',
        codeKey: 'itemGroupCode',
        listExtraParams: organizationIdParams,
        selectorItem: undefined
      },
      'BUDGET_ITEM': {
        listType: 'budget_item',
        labelKey: 'itemName',
        valueKey: 'id',
        codeKey: 'itemCode',
        listExtraParams: organizationIdParams,
        selectorItem: itemSelectorItem
      },
      'CURRENCY': {
        listType: 'new_currency',
        labelKey: 'currencyName',
        valueKey: 'currencyCode',
        codeKey: undefined,
        listExtraParams: {
          baseCurrencyCode: this.props.company.baseCurrency
        },
        selectorItem: undefined
      },
      'COMPANY': {
        listType: 'support_failure_company',
        labelKey: 'name',
        valueKey: 'id',
        codeKey: 'companyCode',
        listExtraParams: {setOfBooksId: this.props.company.setOfBooksId},
        selectorItem: undefined
      },
      'COMPANY_GROUP': {
        listType: 'support_failure_company_group',
        labelKey: 'companyGroupName',
        valueKey: 'id',
        codeKey: 'companyGroupCode',
        listExtraParams: {},
        selectorItem: undefined
      },
      'UNIT': {
        listType: 'support_failure_department',
        labelKey: 'name',
        valueKey: 'id',
        codeKey: 'code',
        listExtraParams: {},
        selectorItem: undefined
      },
      'UNIT_GROUP': {
        listType: 'support_failure_department_group',
        labelKey: 'name',
        valueKey: 'id',
        codeKey: 'deptGroupCode',
        listExtraParams: {},
        selectorItem: undefined
      },
      'EMPLOYEE': {
        listType: 'support_failure_user',
        labelKey: 'fullName',
        valueKey: 'id',
        codeKey: 'id',
        selectorItem: userSelectorItem
      },
      'EMPLOYEE_GROUP': {
        listType: 'support_failure_user_group',
        labelKey: 'name',
        valueKey: 'id',
        codeKey: 'id',
        listExtraParams: {setOfBooksId: this.props.company.setOfBooksId},
        selectorItem: undefined
      }
    };
    this.setState({ searchForm, paramValueMap }, this.getCache);
  }

  //获取缓存数据
  getCache = () => {
    const { budgetBalance } = this.props;
    if (budgetBalance && JSON.stringify(budgetBalance) !== "{}") {
      this.useCondition(budgetBalance)
    }
  };

  //设置缓存数据
  setCache = (values) => {
    const { searchForm } = this.state;
    let cacheParams = values;
    searchForm.map(formItem => {
      switch(formItem.id) {
        case 'versionId':
        case 'structureId':
        case 'scenarioId':
        case 'amountQuarterFlag':
          let idToName = {
            versionId: 'versionName',
            structureId: 'structureName',
            scenarioId: 'scenarioName',
            amountQuarterFlag: 'amountQuarterFlagName'
          };
          let showName = idToName[formItem.id];
          (formItem.options || []).map(optionItem => {
            optionItem.key === cacheParams[formItem.id] && (cacheParams[showName] = optionItem.label)
          });
          return;
        case 'seasonRange':
          formItem.items.map(rangeItem => {
            if (cacheParams[rangeItem.id]) {
              let idToName = {
                quarterLowerLimit: 'quarterLowerLimitName',
                quarterUpperLimit: 'quarterUpperLimitName'
              };
              let showName = idToName[rangeItem.id];
              rangeItem.options.map(optionItem => {
                String(optionItem.key) === cacheParams[rangeItem.id] && (cacheParams[showName] = optionItem.label)
              })
            }
          });
          return;
      }
    });
    configureStore.store.dispatch(setBudgetBalance(cacheParams))
  };

  //渲染下方表格内的选项框及Chooser
  renderColumns = (index, dataIndex) => {
    const { queryLineListTypeOptions, queryLineListParamOptions, params, paramValueMap } = this.state;
    switch(dataIndex){
      case 'type':{
        return (
          <Select placeholder={messages('common.please.select')}
                  onChange={(value) => this.handleChangeType(value, index)}
                  value={params[index].type}
                  notFoundContent={<Spin size="small" />}>
            {queryLineListTypeOptions.map((option)=>{
              return <Option key={option.value}>{option.label}</Option>
            })}
          </Select>
        );
      }
      case 'params':{
        let paramOptions = queryLineListParamOptions[params[index].type];
        return (
          <Select placeholder={messages('common.please.select')}
                  onChange={(value) => this.handleChangeParams(value, index)}
                  value={params[index].params}
                  onFocus={() => this.handleFocusParamSelect(index)}>
            {paramOptions ? paramOptions.map((option)=>{
              return <Option key={option.value}>{option.label}</Option>
            }) : null}
          </Select>
        );
      }
      case 'value':{
        //如果为维度相关项目，则为成本中心selectorItem
        let param = params[index].params ?
          (params[index].type === 'BGT_RULE_PARAMETER_DIM' ? params[index].paramMap : paramValueMap[params[index].params]) :
          null;
        return (
          <Row gutter={20}>
            <Col span={12}>
              <Select value={params[index].isAll ? "1" : "2"}
                      disabled={param === null}
                      onChange={isAll => this.handleChangeIsAll(isAll, index)}>
                <Option value="1">{messages('common.all')}</Option>
                <Option value="2">{messages('budget.key66'/*手动选择*/)}</Option>
              </Select>
            </Col>
            <Col span={12}>
              <Chooser disabled={param === null || params[index].isAll}
                       onChange={value => this.handleChangeValue(value, index)}
                       type={param ? param.listType : null}
                       labelKey={param ? param.labelKey : null}
                       valueKey={param ? param.valueKey : null}
                       listExtraParams={param ? param.listExtraParams : null}
                       selectorItem={param ? param.selectorItem : null}
                       value={params[index].value}
                       showNumber
                       showDetail={false}/>
            </Col>
          </Row>
        )
      }
    }
  };

  //修改参数值为全选还是手动选择
  handleChangeIsAll = (isAll, index) => {
    let { params } = this.state;
    params[index].isAll = isAll === "1";
    if(isAll === "1"){
      params[index].value = [];
    }
    this.setState({ params });
  };

  //修改参数类型，同时清空参数和参数值
  handleChangeType = (value, index) => {
    let { params } = this.state;
    params[index].type = value;
    params[index].params = '';
    params[index].value = [];
    this.setState({ params });
  };

  //修改参数，同时清空参数值
  handleChangeParams = (value, index) => {
    let { params, queryLineListParamOptions } = this.state;
    params[index].params = value;
    params[index].value = [];
    //手动添加成本中心selectorItem所需要的url
    if (params[index].type === 'BGT_RULE_PARAMETER_DIM') {
      let dimensionTypeObj = {};
      let middleParams = {};
      queryLineListParamOptions[params[index].type].map((item) => { dimensionTypeObj[item.value] = item.dimensionType;});
      switch (dimensionTypeObj[value]) {
        case 'CC':
          middleParams['listType'] = 'support_failure_cost_center';
          middleParams['listExtraParams'] = { costCenterId: value, isAll: true };
          middleParams['codeKey'] = 'code';
          break;
        case 'CCGC':
          middleParams['listType'] = 'cost_center_group';
          middleParams['listExtraParams'] = { costCenterGroupCategoryId: value, enabled: true };
          middleParams['codeKey'] = 'costCenterGroupCode';
          break;
      }
      params[index].paramMap = {  //成本中心所需要的selectorItem，url需要在params的onChange里手动添加
        ...middleParams,
        labelKey: 'name',
        valueKey: 'id',
        selectorItem: undefined
      };
      params[index]['parameterSubType'] = dimensionTypeObj[value];
    }
    this.setState({ params });
  };

  //修改参数值
  handleChangeValue = (value, index) => {
    let { params } = this.state;
    params[index].value = value;
    this.setState({ params });
  };

  //点击参数选择框时的回调，若没有对应的值列表则获取
  handleFocusParamSelect = (index, typeParam, length) => {
    let { params, queryLineListParamOptions, paramTypeMap, structureId } = this.state;
    let type = typeParam ? typeParam : params[index].type;
    if(type !== ''){
      if(type === 'BGT_RULE_PARAMETER_DIM'){
        structureId && budgetBalanceService.getRuleParamDIM(structureId).then(res => {
          let options = [];
          res.data.map(data => {
            options.push({
              label: `${data.dimensionName}(${(data.isEnabled || data.enabled)
                ? messages('common.enabled') /*启用*/
                : messages('common.disabled') /*禁用*/})`,
              value: data.dimensionId,
              dimensionType: data.dimensionType
            });
          });
          queryLineListParamOptions[type] = options;
          this.setState({ queryLineListParamOptions });
          if(length){
            this.loadNum++;
            this.setState({ tableLoading: length !== this.loadNum })
          }
        })
      } else {
        if(!queryLineListParamOptions[type] || queryLineListParamOptions[type].length === 0 ){
          this.getSystemValueList(paramTypeMap[type]).then(res => {
            let options = [];
            if(type === 'BGT_RULE_PARAMETER_BUDGET'){
              res.data.values.map(data => {
                data.common && options.push({label: data.messageKey, value: data.code})
              });
            } else {
              res.data.values.map(data => {
                options.push({label: data.messageKey, value: data.code})
              });
            }
            queryLineListParamOptions[type] = options;
            this.setState({ queryLineListParamOptions });
            if(length){
              this.loadNum++;
              this.setState({ tableLoading: length !== this.loadNum })
            }
          });
        } else {
          if(length){
            this.loadNum++;
            this.setState({ tableLoading: length !== this.loadNum })
          }
        }
      }
    }
  };

  //删除下方表格维度项
  deleteItem = (e, index) => {
    let { params } = this.state;
    params.splice(index, 1);
    this.setState({ params });
  };

  //新增维度
  handleNew = () => {
    let { params, paramsKey } = this.state;
    let newParams = {type: '', params: '', value: [], key: paramsKey, isAll: false};
    params.push(newParams);
    paramsKey++;
    this.setState({ params, paramsKey});
  };

  //查询，统一保存为临时方案后跳转
  search = (e) => {
    e.preventDefault();
    this.validate((values) => {
      this.setCache(values);
      this.setState({ searching: true });
      httpFetch.post(`${config.budgetUrl}/api/budget/balance/query/header/user`, values).then(res => {
        this.setState({ searching: false });
        let budgetBalanceResult = menuRoute.getRouteItem('budget-balance-result');
        this.context.router.push(budgetBalanceResult.url.replace(':id', res.data));
      }).catch(e => {
        if(e.response.data){
          message.error(e.response.data.validationErrors ? e.response.data.validationErrors[0].message : e.response.data.message);
        }
        this.setState({ searching: false });
      })
    });
  };

  //验证并打开方案保存窗口
  showSaveModal = () => {
    this.validate(() => {
      const { condition } = this.state;
      if(this.state.condition)
        this.setState({ conditionName: condition.conditionName, conditionCode: condition.conditionCode, showSaveModal: true});
      else
        this.setState({ conditionName: '', conditionCode: '',showSaveModal: true })
    });
  };

  //保存方案
  handleSaveCondition = () => {
    this.validate((values) => {
      values.conditionCode = this.state.conditionCode;
      values.conditionName = this.state.conditionName;
      values.companyId = this.props.company.id;
      this.setState({ saving: true });
      let method;
      if(this.state.saveNewCondition || !this.state.condition)
        method = 'post';
      else {
        method = 'put';
        values.id = this.state.condition.id;
        values.versionNumber = this.state.condition.versionNumber;
      }
      httpFetch[method](`${config.budgetUrl}/api/budget/balance/query/header`, values).then(() => {
        message.success(messages('common.operate.success'));
        this.setState({ showSaveModal: false, saving: false});
      }).catch(e => {
        if(e.response.data){
          message.error(e.response.data.validationErrors ? e.response.data.validationErrors[0].message : e.response.data.message);
          this.setState({ saving: false })
        }
      })
    });
  };

  //验证通过后将state.params的值包装至values
  validate = (callback) => {
    const { costCenterSelectorItem } = this.state;
    this.props.form.validateFieldsAndScroll((err, values) => {
      if(!err){
        const { paramValueMap } = this.state;
        values.queryLineList = [];
        values.periodSummaryFlag = values.periodSummaryFlag === 'TRUE';
        let error = false;
        let nowParams = [];
        this.state.params.map(param => {
          if(param.type === null){
            message.error(messages('budget.key67'/*请选择参数类型*/));
            this.setState({ searching: false });
            error = true;
          }
          if(param.params === null){
            message.error(messages('budget.key68'/*请选择参数*/));
            this.setState({ searching: false });
            error = true;
          }
          if(param.value.length === 0 && !param.isAll){
            message.error(messages('budget.key69'/*当参数值不为全选的时候请选择至少一个参数值*/));
            this.setState({ searching: false });
            error = true;
          }

          if(nowParams.indexOf(param.params) > -1){
            message.error(messages('budget.key70'/*同一参数不能重复添加*/));
            this.setState({ searching: false });
            error = true;
          } else {
            nowParams.push(param.params);
          }

          let queryLine = {
            isAll: param.isAll,
            parameterType: param.type,
            parameterCode: param.params,
            queryParameterList: []
          };
          !param.isAll && param.value.map(value => {
            let paramItem = param.type === 'BGT_RULE_PARAMETER_DIM' ? costCenterSelectorItem : paramValueMap[param.params];
            console.log('value', value, paramItem)
            let queryParameter = {
              parameterValueId: paramItem.listType === 'new_currency' ? null : (paramItem.valueKey ? value[paramItem.valueKey] : null),
              parameterValueCode: paramItem.listType === 'new_currency' ? value[paramItem.valueKey] : (paramItem.codeKey ? value[paramItem.codeKey] : null),
              // parameterValueName: paramItem.labelKey ? value[paramItem.labelKey] : null
              parameterValueName: value.name || value.costCenterGroupName
            };
            queryLine.queryParameterList.push(queryParameter)
          });
          if (param.type === 'BGT_RULE_PARAMETER_DIM') {
            queryLine['parameterSubType'] = param.parameterSubType;
          }
          values.queryLineList.push(queryLine)
        });
        if(error){
          return ;
        }
        values.organizationId = this.state.organizationId;
        values.companyId = this.props.company.id;
        callback(values);
      } else {
        this.setState({ searching: false });
      }
    })
  };

  //应用方案后设置表单值
  setValues = (options) => {
    Object.keys(options).map(key => {
      let searchForm = this.state.searchForm;
      searchForm.map((searchItem, index) => {
        if(searchItem.id === key){
          if(options[key]){
            if((searchItem.type === 'select' || searchItem.type === 'value_list') && typeof options[key] === 'object')
              this.onChangeSelect(searchItem, options[key]);
            else{
              let value = {};
              value[key] = options[key] + '';
              this.props.form.setFieldsValue(value)
            }
          } else {
            let value = {};
            value[key] = null;
            this.props.form.setFieldsValue(value)
          }
        } else if(searchItem.type === 'items'){
          searchItem.items.map(subItem => {
            if(subItem.id === key){
              if(options[key]){
                if((subItem.type === 'select' || subItem.type === 'value_list') && typeof options[key] === 'object')
                  this.onChangeSelect(subItem, options[key], index);
                else {
                  let value = {};
                  value[key] = options[key] + '';
                  this.props.form.setFieldsValue(value)
                }
              } else {
                let value = {};
                value[key] = null;
                this.props.form.setFieldsValue(value)
              }
            }
          })
        }
      });
      searchForm[5].items[0].getParams = searchForm[5].items[1].getParams =
        {setOfBooksId: this.props.company.setOfBooksId, periodYear: options.yearLimit || this.state.periodYear};
      searchForm[5].items[0].options = searchForm[5].items[1].options = [];
      this.setState({ searchForm }, () => {
        configureStore.store.dispatch(setBudgetBalance(null))
      })
    });
  };

  //应用方案，将数据填充至界面
  useCondition = (condition) => {
    this.loadNum = 0;
    this.setState({showSlideFrame : false}, () => {
      if(condition){
        //设置顶部表单的值
        this.setValues({
          versionId: {value: condition.versionId, label: condition.versionName},
          structureId: {value: condition.structureId, label: condition.structureName},
          scenarioId: {value: condition.scenarioId, label: condition.scenarioName},
          yearLimit: condition.yearLimit,
          semiannualLimit:condition.semiannualLimit,
          periodLowerLimit: condition.periodLowerLimit ? {value: condition.periodLowerLimit, label: condition.periodLowerLimit} : null,
          periodUpperLimit: condition.periodUpperLimit ? {value: condition.periodUpperLimit, label: condition.periodUpperLimit} : null,
          periodSummaryFlag: {value: (condition.periodSummaryFlag + '').toUpperCase(), label: condition.periodSummaryFlag ? messages('budget.key71')/*汇总*/ : messages('budget.key72')/*不汇总*/ },
          quarterLowerLimit: condition.quarterLowerLimit ? {value: condition.quarterLowerLimit, label: condition.quarterLowerLimitName} : null,
          quarterUpperLimit: condition.quarterUpperLimit ? {value: condition.quarterUpperLimit, label: condition.quarterUpperLimitName} : null,
          amountQuarterFlag: {value: condition.amountQuarterFlag, label: condition.amountQuarterFlagName}
        });
        this.setState({ structureId: condition.structureId, tableLoading: true }, () => {
          //设置下方列表内的值
          let { paramsKey, paramValueMap } = this.state;
          let params = [];
          condition.queryLineList.length === 0 && this.setState({ tableLoading: false });
          condition.queryLineList.map((item, index) => {
            let newParams = {type: item.parameterType, params: item.parameterCode, value: [], key: paramsKey, isAll: item.isAll};
            item.queryParameterList && item.queryParameterList.map(queryParameter => {
              let val = {};
              let mapItem = item.parameterType === 'BGT_RULE_PARAMETER_DIM' ? this.state.costCenterSelectorItem : paramValueMap[item.parameterCode];
              if(item.parameterCode !== 'CURRENCY')
                val[mapItem.codeKey] = queryParameter.parameterValueCode;
              val[mapItem.valueKey] = item.parameterCode === 'CURRENCY' ? queryParameter.parameterValueCode : queryParameter.parameterValueId;
              val[mapItem.labelKey] = queryParameter.parameterValueName;
              newParams.value.push(val);
            });
            if(item.parameterType === 'BGT_RULE_PARAMETER_DIM'){
              let middleParams = {};
              switch (item.parameterSubType) { // 对成本中心组跟成本中心项做区分处理
                case 'CC':
                  middleParams['listType'] = 'support_failure_cost_center';
                  middleParams['listExtraParams'] = { costCenterId: item.parameterCode, isAll: true };
                  middleParams['codeKey'] = 'code';
                  break;
                case 'CCGC':
                  middleParams['listType'] = 'cost_center_group';
                  middleParams['listExtraParams'] = { costCenterGroupCategoryId: item.parameterCode, enabled: true };
                  middleParams['codeKey'] = 'costCenterGroupCode';
                  break;
              }
              newParams.paramMap = {
                ...middleParams,
                labelKey: 'name',
                valueKey: 'id',
                selectorItem: undefined
              };
              newParams.parameterSubType = item.parameterSubType;
            }
            params.push(newParams);
            paramsKey++;
            //获得参数列的选择项
            this.handleFocusParamSelect(index, item.parameterType, condition.queryLineList.length)
          });
          this.setState({ params, paramsKey, condition, saveNewCondition: true }, () => this.setFieldsByStructureId(true));
        });
      }
    });
  };

  clear = () => {
    this.props.form.resetFields();
    this.setState({ params: [], paramsKey: 0, condition: null, conditionName: '', conditionCode: '' })
  };

  //得到值列表的值增加options
  getValueListOptions = (item) => {
    if(item.options.length === 0 || (item.options.length === 1 && item.options[0].temp)){
      this.getSystemValueList(item.valueListCode).then(res => {
        let options = [];
        res.data.values.map(data => {
          options.push({label: data.messageKey, key: data.code, value: data})
        });
        let searchForm = this.state.searchForm;
        searchForm = searchForm.map(searchItem => {
          if(searchItem.id === item.id)
            searchItem.options = options;
          if(searchItem.type === 'items')
            searchItem.items.map(subItem => {
              if(subItem.id === item.id)
                subItem.options = options;
            });
          return searchItem;
        });
        this.setState({ searchForm });
      })
    }
  };

  //根据接口返回数据重新设置options
  setOptionsToFormItem = (item, url, key) => {
    let params = {};
    if(key){
      params[item.searchKey] = key;
    }
    if( (key !== undefined && key !== '') || key === undefined){
      httpFetch[item.method](url, params).then((res) => {
        let options = [];
        res.data.map(data => {
          options.push({label: data[item.labelKey], key: data[item.valueKey], value: data})
        });
        let searchForm = this.state.searchForm;
        searchForm = searchForm.map(searchItem => {
          if(searchItem.id === item.id)
            searchItem.options = options;
          return searchItem;
        });
        this.setState({ searchForm });
      })
    }
  };

  //给select增加options
  getOptions = (item) => {
    if(item.options.length === 0 || (item.options.length === 1 && item.options[0].temp)){
      let url = item.getUrl;
      httpFetch[item.method](url, item.getParams).then((res) => {
        let options = [];
        res.data.map(data => {
          options.push({label: data[item.labelKey], key: data[item.valueKey], value: data})
        });
        let searchForm = this.state.searchForm;
        searchForm = searchForm.map(searchItem => {
          if(searchItem.id === item.id)
            searchItem.options = options;
          if(searchItem.type === 'items')
            searchItem.items.map(subItem => {
              if(subItem.id === item.id)
                subItem.options = options;
            });
          return searchItem;
        });
        this.setState({ searchForm });
      })
    }
  };

  setFieldsByStructureId = (fromCondition) => {
    let structureId = this.state.structureId;
    structureId && httpFetch.get(`${config.budgetUrl}/api/budget/structures/${structureId}`).then(res => {
      let periodStrategy = res.data.periodStrategy;
      let searchForm = this.state.searchForm;
      searchForm[4].isRequired = periodStrategy === 'SEMIANNUAL';
      searchForm[4].disabled = periodStrategy !== 'SEMIANNUAL';
      searchForm[5].items[0].isRequired = periodStrategy === 'MONTH';
      searchForm[5].items[0].disabled = searchForm[5].items[1].disabled = periodStrategy !== 'MONTH';
      searchForm[5].items[0].options = searchForm[5].items[1].options = [];
      searchForm[6].items[0].isRequired = searchForm[6].items[1].isRequired = periodStrategy === 'QUARTER';
      searchForm[6].items[0].disabled = searchForm[6].items[1].disabled = periodStrategy !== 'QUARTER';
      searchForm[7].isRequired = periodStrategy !== 'YEAR' && periodStrategy !== 'SEMIANNUAL';
      searchForm[7].disabled = periodStrategy === 'YEAR' || periodStrategy === 'SEMIANNUAL';
      if(!fromCondition){
        periodStrategy === 'YEAR' && this.setValues({
          periodLowerLimit: null,
          periodUpperLimit: null,
          quarterLowerLimit: null,
          quarterUpperLimit: null,
          semiannualLimit: null
        });
        periodStrategy === 'MONTH' &&  this.setValues({
          quarterLowerLimit: null,
          quarterUpperLimit: null,
          semiannualLimit: null,
          periodSummaryFlag: {value: 'FALSE', label: messages('budget.key71'/*汇总*/) }
        });
        periodStrategy === 'QUARTER' &&  this.setValues({
          periodLowerLimit: null,
          periodUpperLimit: null,
          semiannualLimit: null,
          periodSummaryFlag: {value: 'FALSE', label: messages('budget.key72'/*不汇总*/) }
        });
      }
      this.setState({ searchForm });
    });
  };

  handleEvent = (value, event) => {
    const { params, queryLineListParamOptions } = this.state;
    switch(event){
      case 'YEAR_CHANGE':
        this.setState({ periodYear: value });
        let searchForm = this.state.searchForm;
        searchForm[5].items[0].getParams = searchForm[5].items[1].getParams = {setOfBooksId: this.props.company.setOfBooksId, periodYear: value};
        searchForm[5].items[0].options = searchForm[5].items[1].options = [];
        this.props.form.setFieldsValue({
          periodLowerLimit: null,
          periodUpperLimit: null
        });
        this.setState({ searchForm });
        break;
      case 'STRUCTURE_CHANGE':
        this.setState({ tableLoading: true }, () => {
          value && httpFetch.get(`${config.budgetUrl}/api/budget/structure/assign/layouts/queryAll?structureId=${value}`).then(res => {
            let options = [];
            res.data.map(data => {
              options.push({
                label: `${data.dimensionName}(${(data.isEnabled || data.enabled) ? messages('common.enabled') /*启用*/
                  : messages('common.disabled') /*禁用*/})`,
                value: data.dimensionId,
                dimensionType: data.dimensionType
              })
            });
            queryLineListParamOptions['BGT_RULE_PARAMETER_DIM'] = options;
            this.setState({ queryLineListParamOptions }, () => {
              let targetParams = [];
              params.map(item => {
                if(item.type === 'BGT_RULE_PARAMETER_DIM') {
                  let have = false;
                  options.map(option => {
                    have = have || option.value === item.params;
                  });
                  have && targetParams.push(item)
                } else {
                  targetParams.push(item)
                }
              });
              this.setState({ structureId : value, params: targetParams, tableLoading: false }, () => {
                this.setFieldsByStructureId()
              });
            });
          });
        });
        break;
    }
  };

  onChangeSelect = (item, value, index) => {
    let valueWillSet = {};
    let { searchForm }  = this.state;
    if(index !== undefined){
      searchForm[index].items = searchForm[index].items.map(searchItem => {
        if(searchItem.id === item.id){
          valueWillSet[searchItem.id] = value.value + '';
          if(searchItem.options.length === 0 || (searchItem.options.length === 1 && searchItem.options[0].temp)){
            let dataOption = {};
            dataOption[item.type === 'value_list' ? 'code' : item.valueKey] = value.value;
            dataOption[item.type === 'value_list' ? 'messageKey' : item.labelKey] = value.label;
            searchItem.options = [{label: value.label, key: value.value, value: dataOption, temp: true}];
          }
        }
        return searchItem;
      });
    } else {
      searchForm = searchForm.map(searchItem => {
        if(searchItem.id === item.id){
          valueWillSet[searchItem.id] = value.value + '';
          if(searchItem.options.length === 0 || (searchItem.options.length === 1 && searchItem.options[0].temp)){
            let dataOption = {};
            dataOption[item.type === 'value_list' ? 'code' : item.valueKey] = value.value;
            dataOption[item.type === 'value_list' ? 'messageKey' : item.labelKey] = value.label;
            searchItem.options = [{label: value.label, key: value.value, value: dataOption, temp: true}];
          }
        }
        return searchItem;
      });
    }
    this.setState({ searchForm }, () => {
      this.props.form.setFieldsValue(valueWillSet);
    });
    let handle = item.event ? (event) => this.handleEvent(event,item.event) : ()=>{};
    handle();
  };

  //渲染搜索表单组件
  renderFormItem(item){
    let handle = item.event ? (event) => this.handleEvent(event, item.event) : ()=>{};
    switch(item.type){
      //选择组件
      case 'select':{
        const someOptionsDisable =  ['versionId','structureId','scenarioId'].indexOf(item.id) > -1;
        return (
          <Select placeholder={messages('common.please.select')}
                  onChange={handle}
                  disabled={item.disabled}
                  firstActiveValue={item.activeValue}
                  allowClear
                  onFocus={item.getUrl ? () => this.getOptions(item) : () => {}}>
            {item.options.map((option)=>{
              return <Option style={someOptionsDisable && !(option.value && option.value.isEnabled) && { color:'#cecece' }}
                             key={ '' + option.key }>{option.label}</Option>
            })}
          </Select>
        )
      }
      //值列表选择组件
      case 'value_list':{
        return (
          <Select placeholder={messages('common.please.select')}
                  onChange={handle}
                  disabled={item.disabled}
                  allowClear
                  onFocus={() => this.getValueListOptions(item)}>
            {item.options.map((option)=>{
              return <Option key={'' + option.key}>{option.label}</Option>
            })}
          </Select>
        )
      }
      //同一单元格下多个表单项组件
      case 'items':{
        return (
          <Row gutter={10} key={item.id}>
            {item.items.map(searchItem => {
              return (
                <Col span={parseInt(24 / item.items.length)} key={searchItem.id}>
                  <FormItem label={searchItem.label} colon={false}>
                    {this.props.form.getFieldDecorator(searchItem.id, {
                      initialValue: searchItem.defaultValue,
                      rules: [{
                        required: searchItem.isRequired,
                        message: messages('common.can.not.be.empty', {name: searchItem.label}),  //name 不可为空
                      }]
                    })(
                      this.renderFormItem(searchItem)
                    )}
                  </FormItem>
                </Col>
              )}
            )}
          </Row>
        )
      }
    }
  }

  getFields(){
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {};
    const children = [];
    this.state.searchForm.map((item, i)=>{
      children.push(
        <Col span={8} key={item.id}>
          {item.type === 'items' ? this.renderFormItem(item) :
            <FormItem {...formItemLayout} label={item.label} colon={false}>
              {getFieldDecorator(item.id, {
                initialValue: item.defaultValue,
                rules: [{
                  required: item.isRequired,
                  message: messages('common.can.not.be.empty', {name: item.label}),  //name 不可为空
                }]
              })(
                this.renderFormItem(item)
              )}
            </FormItem>
          }
        </Col>
      );
    });
    return children;
  }

  handleChangeConditionCode = (e) => {
    this.setState({ conditionCode: e.target.value })
  };

  render(){
    const { params, columns, showSlideFrame, showSaveModal, saving,
      condition , conditionCode, conditionName, saveNewCondition, searching, tableLoading } = this.state;

    return (
      <div className="budget-balance">
        <Form
          className="ant-advanced-search-form"
          onSubmit={this.search}
        >
          <div className="base-condition">
            <div className="base-condition-title">{messages('budget.key73'/*基本条件*/)}</div>
            <Row gutter={40} className="base-condition-content" type="flex" align="top">{this.getFields()}</Row>
          </div>
          <div className="table-header">
            <div className="table-header-title">{messages('budget.key78'/*查询维度*/)}</div>
            <div className="table-header-buttons">
              <Button onClick={this.handleNew} disabled={params.length === 20}>{messages('budget.key79'/*添加*/)}</Button>
            </div>
          </div>
          <Table columns={columns}
                 dataSource={params}
                 bordered
                 loading={tableLoading}
                 pagination={false}
                 size="middle"/>
          <Affix offsetBottom={0} className="bottom-bar">
            <Button type="primary" htmlType="submit" loading={searching}>{messages('budget.key74'/*查询*/)}</Button>
            <Button className="button-reset" onClick={this.clear}>{messages('budget.key75'/*重置*/)}</Button>
            <Button className="button-save" onClick={this.showSaveModal}>{messages('budget.key76'/*保存方案*/)}</Button>
            <Button onClick={() => {this.setState({showSlideFrame : true})}}>{messages('budget.key38'/*应用现有方案*/)}</Button>
            <If value={condition}>
              <div className="condition-name">{messages('budget.key77'/*已应用：*/)}{(condition || {}).conditionName}</div>
            </If>
          </Affix>
        </Form>

        <SlideFrame content={BudgetBalanceCondition}
                    title={messages('budget.key80'/*我的方案*/)}
                    show={showSlideFrame}
                    onClose={() => this.setState({showSlideFrame : false})}
                    afterClose={this.useCondition}/>

        <Modal title={messages('budget.key76'/*保存方案*/)}
               visible={showSaveModal}
               onCancel={() => this.setState({ showSaveModal: false })}
               onOk={this.handleSaveCondition}
               confirmLoading={saving}>
          <div className="save-modal-content">
            <div>{messages('budget.key35'/*方案代码*/)}</div>
            <Input onChange={this.handleChangeConditionCode} value={conditionCode}/>
            <div>{messages('budget.key36'/*方案名称*/)}</div>
            <Input onChange={e => this.setState({ conditionName: e.target.value })} value={conditionName}/>
            <br/>
            <If value={condition}>
              <Checkbox checked={saveNewCondition}
                        onChange={e => this.setState({ saveNewCondition: e.target.checked })}>
                {messages('budget.key81'/*保存为新方案*/)}
              </Checkbox>
            </If>
          </div>
        </Modal>
      </div>
    )
  }

}

function mapStateToProps(state) {
  return {
    company: state.login.company,
    organization: state.login.organization,
    budgetBalance: state.cache.budgetBalance,
  }
}

BudgetBalance.contextTypes = {
  router: React.PropTypes.object
};


const WrappedBudgetBalance = Form.create()(BudgetBalance);

export default connect(mapStateToProps)(WrappedBudgetBalance);
